package com.cskaoyan.app.service.impl;

import com.cskaoyan.app.bean.common.BasePageInfo;
import com.cskaoyan.app.bean.common.CommonData;
import com.cskaoyan.app.bean.po.MarketKeyword;
import com.cskaoyan.app.bean.po.MarketKeywordExample;
import com.cskaoyan.app.mapper.MarketKeywordMapper;
import com.cskaoyan.app.service.AdminKeywordService;
import com.cskaoyan.app.util.StringsUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Random;

/**
 * @author Shuuka
 * @email 1712430536@qq.com
 * @date 2022/12/29 22:45
 */
@Service
public class AdminKeywordServiceImpl implements AdminKeywordService {

    @Autowired
    MarketKeywordMapper marketKeywordMapper;

    @Override
    public CommonData<MarketKeyword> list(BasePageInfo basePageInfo, String keyword, String url) {
        PageHelper.startPage(basePageInfo.getPage(), basePageInfo.getLimit());
        MarketKeywordExample example = new MarketKeywordExample();
        example.setOrderByClause(StringsUtil.getOrderClause(basePageInfo.getSort(), basePageInfo.getOrder()));
        MarketKeywordExample.Criteria criteria = example.createCriteria();
        if (!StringsUtil.isEmpty(keyword)) {
            criteria.andKeywordLike("%" + keyword + "%");
        }
        if (!StringsUtil.isEmpty(url)) {
            criteria.andUrlLike("%" + url + "%");
        }
        criteria.andDeletedEqualTo(false);
        List<MarketKeyword> marketKeywords = marketKeywordMapper.selectByExample(example);
        PageInfo<MarketKeyword> marketKeywordPageInfo = new PageInfo<>(marketKeywords);
        return CommonData.data(marketKeywordPageInfo);
    }

    @Override
    public MarketKeyword update(MarketKeyword marketKeyword) {
        marketKeyword.setUpdateTime(new Date());
        Integer i = marketKeywordMapper.updateByPrimaryKeySelective(marketKeyword);
        if (i != 1) {
            return null;
        }
        marketKeyword = marketKeywordMapper.selectByPrimaryKey(marketKeyword.getId());
        return marketKeyword;
    }

    @Override
    public MarketKeyword create(MarketKeyword marketKeyword) {
        Date date = new Date();
        marketKeyword.setAddTime(date);
        marketKeyword.setUpdateTime(date);
        marketKeyword.setDeleted(false);
        Integer row = marketKeywordMapper.insertSelective(marketKeyword);
        if (row != 1) {
            return null;
        }
        marketKeyword = marketKeywordMapper.selectByPrimaryKey(marketKeyword.getId());
        return marketKeyword;
    }

    @Override
    public Integer delete(MarketKeyword marketKeyword) {
        marketKeyword.setDeleted(true);
        marketKeyword.setUpdateTime(new Date());
        MarketKeywordExample example = new MarketKeywordExample();
        MarketKeywordExample.Criteria criteria = example.createCriteria();
        criteria.andIdEqualTo(marketKeyword.getId());
        Integer i = marketKeywordMapper.updateByExampleSelective(marketKeyword,example);
        return i;
    }
}
